package com.hrt.test.bcsite;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.btcode.orm.model.ModelPageResult;
import org.junit.Test;

import com.btcode.common.PropertiesUnit;
import com.btcode.db.executer.IDataExecuter;
import com.btcode.db.result.Row;
import com.btcode.db.sqlparameter.SQLParameterList;
import com.btcode.db.wherefilter.WhereFilterType;
import com.btcode.orm.conver.DefaultModelConver;
import com.btcode.orm.conver.IModelConver;
import com.btcode.orm.executer.IORMExecuter;
import com.btcode.orm.filter.FilterGroup;
import com.btcode.orm.filter.FilterSingle;
import com.btcode.orm.filter.IFilter;
import com.btcode.orm.model.generater.ModelGenerater;
import com.btcode.orm.model.generater.SQLLetGenerater;
import com.hrt.datasender.datadao.model.BcSiteModel;
import com.hrt.datasender.datadao.model.BdSourceModel;
import com.hrt.db.DBManager;
import com.hrt.test.db.CycledataModel;

public class BCSite {

    public BCSite() {
        PropertiesUnit.setPropertiesPath("E:/workspace/DataSenderV2/DataSenderV2/resource/");
    }

    /**
     * 生成model
     * 
     * @throws IOException
     */

    public void generateModel() throws IOException {
        IDataExecuter sqlletExecuter = DBManager.getDataExecuter();

        ModelGenerater generater = new SQLLetGenerater(sqlletExecuter);

        generater.generaterFile("com.hrt.datasender.web.bcsite", "BC_SITE",
                "E:/workspace/DataSenderV2/DataSenderV2/web/com/hrt/datasender/web/bcsite/");

    }

    /**
     * 插入model
     */
    public void insertModel() {

        IORMExecuter ormExecuter = DBManager.getORMExecuter();

        BcSiteModel model = new BcSiteModel();
        model.setName("福田子站");
        model.setSystemType("21");

        ormExecuter.create(model);
    }

    /**
     * 获取model
     */
    public void getModel() {

        IORMExecuter ormExecuter = DBManager.getORMExecuter();

        CycledataModel cycleModel = new CycledataModel();
        cycleModel.setDataid("2017-10-31 16:10:34");

        cycleModel = ormExecuter.getModel(cycleModel);

        System.out.println(cycleModel);

    }

    /**
     * 更新model
     */
    public void updateModel() {
        IORMExecuter ormExecuter = DBManager.getORMExecuter();

        CycledataModel cycleModel = new CycledataModel();
        cycleModel.setDataid("2017-10-31 16:10:34");
        cycleModel.setPollname("change" + Calendar.getInstance().getTimeInMillis());

        ormExecuter.update(cycleModel);
    }

    /**
     * 删除model
     */
    public void deleteModel() {
        IORMExecuter ormExecuter = DBManager.getORMExecuter();

        CycledataModel cycleModel = new CycledataModel();
        cycleModel.setDataid("2017-10-31 16:10:34");

        ormExecuter.delete(cycleModel);
    }

    /**
     * 执行sql
     */
    public void executeSQL() {
        IDataExecuter sqlletExecuter = DBManager.getDataExecuter();

        String sql = " SELECT * FROM  CycleData WHERE CREATETIME > @createtime";

        Calendar cal = Calendar.getInstance();
        cal.set(2017, 9, 10);

        SQLParameterList pc = new SQLParameterList();
        pc.put("createtime", cal.getTime());

        List<Row> rows = sqlletExecuter.queryList(sql, pc);

        System.out.println(rows);
    }

    /**
     * 执行sql
     */
    public void executeDateSQL() {

        IORMExecuter ormExecuter = DBManager.getORMExecuter();

        String sql = " SELECT * FROM BD_SOURCE WHERE DATATIME > @start ";

        Calendar cal = Calendar.getInstance();

        SQLParameterList pc = new SQLParameterList();
        cal.set(2017, 10, 3, 12, 40, 0);
        pc.put("start", cal.getTime());

        List<BdSourceModel> models = ormExecuter.getModelListSQL(sql, pc, BdSourceModel.class);

        System.out.println(models);
    }

    public void executeModelFilter() {

        IORMExecuter ormExecuter = DBManager.getORMExecuter();

        List<IFilter> filters = new ArrayList<>();
        FilterGroup filter = new FilterGroup();

        Calendar cal = Calendar.getInstance();
        cal.set(2017, 9, 10);
        filter.addFilter(new FilterSingle(BdSourceModel.datatime, WhereFilterType.GREATER_EQUAL_THAN, cal.getTime()));
        cal.set(2017, 9, 11);
        filter.addFilter(new FilterSingle(BdSourceModel.datatime, WhereFilterType.LESS_EQUAL_THAN, cal.getTime()));

        ModelPageResult mpr = ormExecuter.getModelList(BdSourceModel.class, filters,null);
        List<BdSourceModel> models = mpr.getData();

        System.out.println(models);
    }

    /**
     * 结果集转model集
     */
    public void converRowsToModel() {
        IDataExecuter sqlletExecuter = DBManager.getDataExecuter();
        String sql = " SELECT * FROM  CycleData";
        List<Row> rows = sqlletExecuter.queryList(sql, null);

        IModelConver modelConver = new DefaultModelConver();

        List<CycledataModel> models = modelConver.converToORMModels(rows, CycledataModel.class);

        System.out.println(models);
    }

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    @Test
    public void test() {
        byte[] data = hexStringToBytes("0A");
//        System.out.println(data);
    }
}
